<?php
/**
 * @version		$Id: en-GB.com_k2store.ini 329 2010-04-14 05:39:21Z weblogicxindia $
 * @package		K2Store
 * @author    Weblogicx India http://www.weblogicxindia.com
 * @copyright	Copyright (c) 2010 - 2015 Weblogicx India Ltd. All rights reserved.
 * @license		GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
 */


// no direct access
defined('_JEXEC') or die ('Restricted access');
JLoader::register('K2Plugin', JPATH_ADMINISTRATOR.DS.'components'.DS.'com_k2'.DS.'lib'.DS.'k2plugin.php');

/**
 * Example K2 Plugin to render YouTube URLs entered in backend K2 forms to video players in the frontend.
 */

class plgK2K2Store extends K2Plugin {

	// Some params
	var $pluginName = 'k2store';
	var $pluginNameHumanReadable = 'K2 Store';

	function plgK2K2Store( & $subject, $params) {
	
		parent::__construct($subject, $params);
	}

	
	function onK2AfterDisplay( & $item, & $params, $limitstart) {
	
		global $mainframe;
		$lang = JFactory::getLanguage();
		$lang->load('com_k2store');
		
		$plugins = new K2Parameter($item->plugins, '', $this->pluginName);
		$item_price = $plugins->get('item_price');
		if ($item_price) {
			
			 $doc = &JFactory::getDocument();
	//		 $doc->addScript(JURI::base().'components'.DS.'com_k2store'.DS.'js'.DS.'moodalbox.js');
			 $doc->addStyleSheet(JURI::base().'components'.DS.'com_k2store'.DS.'css'.DS.'style.css');
	//		 $doc->addStyleSheet(JURI::base().'components'.DS.'com_k2store'.DS.'css'.DS.'moodalbox.css');
			
			$doc->addCustomTag("<script type=\"text/javascript\">
		   window.addEvent('domready', function() {
		      SqueezeBox.initialize({});
		      $$('.cart_form').each(function(el) {
		         el.addEvent('submit', function(e) {
		            new Event(e).stop();
		            var quantity = this.quantity.value;
		            var url = this.action+'&quantity='+quantity+'&Itemid='+this.Itemid.value;
		            SqueezeBox.fromElement(el, {url:url});
		         });
		      });
		   }); 
		 </script>");
			
			$plugin = & JPluginHelper::getPlugin('k2', 'k2store');
			$pluginParams = new JParameter($plugin->params);
			
			$content = $this->addtoCart($item);
			
		} else {
			$content = '';
			
		}
		return $content;
	}
	
	function onK2AfterDisplayContent( & $item, & $params, $limitstart) {
	
		global $mainframe;
	
		$plugin = & JPluginHelper::getPlugin('k2', 'k2store');
		$pluginParams = new JParameter($plugin->params);
		$plugins = new K2Parameter($item->plugins, '', $this->pluginName);
		$item_price = $plugins->get('item_price');
		if ( empty($item_price)) return;
		
		if ($item_price) {
		//$output = '<div class="ct"><div class="lx"></div>';
		/* $output ='
		<span class="lb">Loại xe &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : </span><span class="it_lb">'.$item->type.'</span><br /> */
		$output =''; /* <br />
		<span class="lb">Giá thuê xe : </span><a href=index.php?option=com_k2&view=item&id='.$item->id.':'.$item->alias.'&Itemid=4><span class="it_lb">Click xem chi tiết</span></a>
		'; */
		
		//$output .= '<div class="lx"></div>';
		}
	
		return $output;
	}	
	
 function addtoCart(&$item) {
	 
		$user 	=& JFactory::getUser();
		$plugin = & JPluginHelper::getPlugin('k2', 'k2store');
		$pluginParams = new JParameter($plugin->params);
		$plugins = new K2Parameter($item->plugins, '', $this->pluginName);
		$item_price = $plugins->get('item_price');
		$isregister = $pluginParams->get('isregister');
		
		$Itemid = intval( JRequest::getVar( 'Itemid', 0) );
		
		$link = JRoute::_('index.php?option=com_k2store&task=checkout&Itemid='.$Itemid);
		$currency = $pluginParams->get('currency');
		$non_user_text = JText::_('ONLY REGISTERED');
		$login_link = JRoute::_('index.php?option=com_user&view=login');
		$add_to_cart_text = JText::_('ADD TO CART');
		$checkout_text = JText::_('CHECKOUT');
		//get the current url 
		
		$redirectUrl = base64_encode($item->link); 
		$redirectUrl = $login_link.'&return='.$redirectUrl;
		
		if ($isregister) {
			if($user->id){
					$reg_link ="<input type=\"button\" value=\"$checkout_text \" onclick=\"window.location = '$link'\"/>";
			 } else {
					$reg_link ="<a href=\"$redirectUrl\"> $non_user_text</a>";
			}	 	
			
		} else {		
				$reg_link ="<input type=\"button\" value=\"$checkout_text \" onclick=\"window.location = '$link'\"/>";				
		}
		
		//build the cart buttons
		
			if ($item) {
				
				if ($item_price){
					$cart = "<div class=\"mycart\">";
					$cart .="<form id=\"cart_form\" class=\"cart_form\" action=\"index.php?option=com_k2store&task=cartaction&order_code=$item->id&Itemid=$Itemid\" method=\"get\">";
					$cart .="<input type=\"hidden\" name=\"option\" value=\"com_k2store\" />";
					$cart .="<input type=\"hidden\" name=\"task\" value=\"cartaction\" />";
					$cart .="<input type=\"hidden\" name=\"Itemid\" value=\"$Itemid\" />";
					$cart .="<input type=\"hidden\" id=\"order_code\" name=\"order_code\" value=\"$item->id\" />";
					$cart .="<label><b> $item->title : </b> <input class=\"center\" type=\"text\" id=\"quantity\" name=\"quantity\" value=\"1\" size=\"3\" /></label>";
					$cart .="<label><b> $currency </b> $item_price </label>";
					$cart .="<input type=\"submit\" name=\"submit\" value=\"$add_to_cart_text\" />";
					$cart .="&nbsp;&nbsp;&nbsp;";
					$cart .= $reg_link;
					
					$cart .="</form>";
					$cart .="</div>";
				}	
			}	
							
	return $cart;
	
}	

	
	

	
} // END CLASS

